package com.ruoyi.fetv.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.fetv.controller.vo.Label;
import com.ruoyi.fetv.controller.vo.navigation.AddReq;
import com.ruoyi.fetv.controller.vo.navigation.EditReq;
import com.ruoyi.fetv.controller.vo.navigation.RespVO;
import com.ruoyi.fetv.domain.FetvNavigationContext;
import com.ruoyi.fetv.mapper.FetvNavigationContextMapper;
import com.ruoyi.fetv.service.IFetvNavigationContextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 栏目Service业务层处理
 *
 * @author Jack
 * @date 2023-03-07
 */
@Service
public class FetvNavigationContextServiceImpl implements IFetvNavigationContextService {
    @Autowired
    private FetvNavigationContextMapper fetvNavigationContextMapper;

    /**
     * 查询栏目
     *
     * @param id 栏目主键
     * @return 栏目
     */
    @Override
    public RespVO selectFetvNavigationContextById(Long id) {
        RespVO respVO = fetvNavigationContextMapper.selectFetvNavigationContextById(id);
        if (respVO.getParentId() != 0) {
            respVO.setParentName(fetvNavigationContextMapper.getNavigationNameById(respVO.getParentId()));
        }
        return respVO;
    }

    /**
     * 查询栏目列表
     *
     * @param fetvNavigationContext 栏目
     * @return 栏目
     */
    @Override
    public List<RespVO> selectFetvNavigationContextList(FetvNavigationContext fetvNavigationContext) {
        List<RespVO> respVOList = fetvNavigationContextMapper.selectFetvNavigationContextList(fetvNavigationContext);
        for (RespVO respVO : respVOList) {
            if (respVO.getParentId() != 0) {
                respVO.setParentName(fetvNavigationContextMapper.getNavigationNameById(respVO.getParentId()));
            }
        }
        return respVOList;
    }

    /**
     * 新增栏目
     *
     * @param fetvNavigationContext 栏目
     * @return 结果
     */
    @Override
    public int insertFetvNavigationContext(AddReq fetvNavigationContext) {
        fetvNavigationContext.setCreateTime(DateUtils.getNowDate());
        return fetvNavigationContextMapper.insertFetvNavigationContext(fetvNavigationContext);
    }

    /**
     * 修改栏目
     *
     * @param fetvNavigationContext 栏目
     * @return 结果
     */
    @Override
    public int updateFetvNavigationContext(EditReq fetvNavigationContext) {
        fetvNavigationContext.setUpdateTime(DateUtils.getNowDate());
        return fetvNavigationContextMapper.updateFetvNavigationContext(fetvNavigationContext);
    }

    /**
     * 批量删除栏目
     *
     * @param ids 需要删除的栏目主键
     * @return 结果
     */
    @Override
    public int deleteFetvNavigationContextByIds(Long[] ids) {
        for (Long id : ids) {
            int result = fetvNavigationContextMapper.hasChildren(id);
            if (result != 0) {
                throw new ServiceException("id为" + id + "的节点下有子节点不能被删除");
            }
        }
        return fetvNavigationContextMapper.deleteFetvNavigationContextByIds(ids);
    }

    /**
     * 删除栏目信息
     *
     * @param id 栏目主键
     * @return 结果
     */
    @Override
    public int deleteFetvNavigationContextById(Long id) {
        return fetvNavigationContextMapper.deleteFetvNavigationContextById(id);
    }

    @Override
    public List<RespVO> selectFetvNavigationContextListLevel(FetvNavigationContext fetvNavigationContext) {
        if (fetvNavigationContext.getParentId() == null) {
            fetvNavigationContext.setParentId(0L);
        }
        return fetvNavigationContextMapper.selectFetvNavigationContextList(fetvNavigationContext);
    }

    @Override
    public String getNameById(Long id) {
        return fetvNavigationContextMapper.getNavigationNameById(id);
    }

    @Override
    public List<Label> getIdAndNameByName(String name) {
        return fetvNavigationContextMapper.getIdAndNameByName(name);
    }
}
